package com.study.example.gameserver.module.backpack.use.handler.impl;

import com.sparrow.common.handler.Handler;
import com.sparrow.common.handler.HandlerManager;
import com.study.example.gameserver.module.backpack.use.ItemUseContext;
import com.study.example.gameserver.module.backpack.use.ItemUseHandler;
import com.study.example.gameserver.module.backpack.use.handler.AbstractItemUseHandler;
import com.study.example.gameserver.module.backpack.use.handler.ItemUseWithStateHandler;
import com.study.example.gameserver.module.player.entity.Player;
import com.study.example.gameserver.module.player.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AddExpHandler extends AbstractItemUseHandler {

    @Autowired
    PlayerService playerService;

    @Override
    public ItemUseWithStateHandler createHandler(String useParam) {
        return new AddExpWithStateHandler(useParam);
    }

    @Override
    public String getHandlerKey() {
        return ItemUseHandler.AddExp;
    }

    /**
     * 带状态的处理器
     */
    public class AddExpWithStateHandler implements ItemUseWithStateHandler {
        /**
         * 每个经验道具加多少经验
         */
        private int perExp;

        public AddExpWithStateHandler(String useParam) {
            this.perExp = Integer.valueOf(useParam);
        }

        public boolean handle(ItemUseContext context) {
            Player player = context.getPlayer();
            playerService.addExp(player, perExp * context.getNum());
            return true;
        }
    }


    public static void main(String[] args) {
        HandlerManager register = new HandlerManager();
        register.register(new AddExpHandler());

        Handler handler = register.getHandler(ItemUseHandler.class, ItemUseHandler.AddExp);
        System.out.println(handler);
    }

}
